<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui-mobile/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui-mobile/themes/mobile.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/static/jquery-easyui-mobile/themes/icon.css"/>">


<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui-mobile/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui-mobile/jquery.easyui.mobile.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui-mobile/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.panel.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.datagrid.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/plugins/jquery.messager.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/ext-for-framework.js.jsp"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/jquery-easyui/locale/easyui-lang-zh_CN.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/static/js/common.js"/>"></script>


<script type="text/javascript">


    function initList(listDisplayEId) {
        $('#' + listDisplayEId).datagrid({
        	  url : "<c:url value='/order/findcustMaterDetailed.do'/>" +"?oid=${ordId }&supId=${supId }"
        });
    };
    function searchInfo(){
        var date = $('#ordDate').datebox('getValue');
            $('#dg').datagrid('reload', {
                ordDate:date
            }); 
            
    }
    $(document).ready(function() {
        initList('dg');
    });
        
    var editIndex = undefined;
    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#dg').datagrid('validateRow', editIndex)){
            $('#dg').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }

    function onClickRow(index){
        if (editIndex != index){
            if (endEditing()){
                $('#dg').datagrid('selectRow', index)
                        .datagrid('beginEdit', index);
                editIndex = index;
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }
    function removeit() {
        if (editIndex == undefined) {
            return
        }
        $('#dg').datagrid('cancelEdit', editIndex).datagrid('deleteRow',
                editIndex);
        editIndex = undefined;
    }
    //检索最后出货价格
    function checkvalue(){
    	alert("微信暂不支持！敬请期待！");
    }
    //导入数量
    function updateMaterWeightAndValue(){
        var rows = $('#dg').datagrid('getRows');
            //生成json数据
            var turnoverList = new Array();
            var jsonFater;

            for (var i = 0; i < rows.length; i++) {
                var rowIndex = $('#dg').datagrid('getRowIndex',
                        rows[i]['ordSupMaterId']);
                $('#dg').datagrid('endEdit', rowIndex);
                
                
                var jsonSun = "{materId:'" + rows[i]['ordMaterId'] + "',materLevel:'"
                        + rows[i]['ordMaterLevel'] + "',materWeight:'"
                        + rows[i]['ordMaterWeightReal'] 
                        + "',materValue:'" + rows[i]['ordMaterValueRealUnit'] 
                        + "',materValueUndressed:'" + rows[i]['ordMaterValueRealCostUnit']
                        + "',date:'"+ rows[i]['ordDate'] + "'}";
                turnoverList.push(jsonSun);
                
            }
            //beginEditList(rows, '#dg', 'ordSupMaterId');

            jsonFater = JSON.stringify(turnoverList);
            $("#materJson").attr("value", jsonFater);
            var form = new FormData();
            //ajax 提交表单数据
            form.append("date", rows[0]['ordDate']); // 可以增加表单数据
            form.append("custId", rows[0]['ordCustId']); // 可以增加表单数据

            form.append("materJson", jsonFater); // 可以增加表单数据
            // XMLHttpRequest 对象
            xhr = new XMLHttpRequest();
            xhr.open("post", '<c:url value='/supplier/importSupMaterRealityWeightForWeb.do'/>', true);
            xhr.onload = function(a) {
                $.messager.alert('系统提示', '修改成功', 'info', function() {
                    $('#'+listDisplayEId).datagrid('reload');
                    $('#'+listDisplayEId).datagrid('clearSelections');
                });
            }; 
            xhr.send(form);
        
    }
    
    //检索最后一次出货价格
    function searchLastShipmentValue(){
        var rows = $('#dg').datagrid('getRows');
        if(rows.length == 0){
            $.messager.alert('提示','请选择一条记录操作！','info');
            return;
        } else {
            for (var i = 0; i < rows.length; i++) {
                var rowIndex = $('#dg').datagrid('getRowIndex',
                        rows[i]['ordSupMaterId']);
                $('#dg').datagrid('endEdit', rowIndex);
                if(rows[i]['periodValueFlag'] ==1){
                    continue;
                }
                rows[i]['ordMaterValueRealUnit'] = getLastShipmentValue(rows[i]['ordMaterId'],rows[i]['ordCustId']);
                rows[i]['ordMaterValueReal'] = decimal(rows[i]['ordMaterValueRealUnit']*rows[i]['ordMaterWeightReal'],2);
                //alert(i);
                $('#dg').datagrid('refreshRow', rowIndex);
                //alert(rowIndex);
                //$('#dg').datagrid('beginEdit', rowIndex);
            }
        }
        alert("检索完毕");
    }
    function getLastShipmentValue(materId,ordCustId) {
        var returnString;
        $.ajax({
            async : false,
            url : "<c:url value='/materSup/getLastShipmentValue.do'/>" + "?materId=" + materId +"&shopId=" + ordCustId,
            type : "post",
            success : function(data) {
                if (typeof (data) == 'string') {
                    data = JSON.parse(data);
                }
                returnString = data.materValue;
            }
        });
        return returnString;
    }
</script>
<body>
	<table id="dg"
		data-options="
                header:'#hh',
                iconCls: 'icon-edit',
                singleSelect: true,
                fit:true,
                fitColumns:true,
                border: false,
                nowrap : false,
                scrollbarSize: 0,
                rownumbers:true,
                onClickRow:onClickRow,
                idField:'ordSupMaterId'
            ">
		<thead>
			<tr>
				<th data-options="field:'id',hidden:true">序号</th>
				<th data-options="field:'ordSupMaterId',hidden:true">材料序号</th>
				<th data-options="field:'ordId',hidden:true">订单号</th>
				<th data-options="field:'ordMaterId',hidden:true">材料id</th>
				<th data-options="field:'ordMaterName',width:80">材料名</th>
				<th data-options="field:'ordMaterLevel',hidden:true">材料品级</th>
				<th data-options="field:'brands',width:30">品牌</th>
				<th data-options="field:'specifications',width:50">规格</th>
				<th data-options="field:'ordMaterWeight',width:30,hidden:true">需求量</th>
				<th
					data-options="field:'ordMaterWeightReal',width:30,editor:{  type:'numberbox', options: {   precision:2  }  }">数量</th>
				<th data-options="field:'unit',width:30">单位</th>
				<th
					data-options="field:'ordMaterValueRealUnit',width:30,editor:{  type:'numberbox', options: {   precision:2  }  }  ">单价</th>
				<th data-options="field:'ordMaterValueReal',width:30">总价</th>

			</tr>
		</thead>
	</table>
	<div id="hh">
		<div class="m-toolbar">
			<div class="m-left">
                <li style="line-height: 32px; font-size: 16px; font-weight: bold;" value="${shopName }">${shopName }
                    
                </li>
            </div>
			<div class="m-right">
			    <a href="javascript:void(0)" class="easyui-menubutton" data-options="menu:'#mm1',iconCls:'icon-edit',outline:true" style="width:70px">操作</a>  
				<!-- <a href="javascript:void(0)" class="easyui-linkbutton"
					data-options="iconCls:'icon-save',plain:true"
					onclick="updateMaterWeightAndValue()">提交数量和单价</a>  -->
			</div>
		</div>
	</div>
    <div id="mm1" class="easyui-menu" style="width:150px;">
        <div data-options="iconCls:'icon-save'" onclick="updateMaterWeightAndValue()">提交数量和单价</div>
        <div data-options="iconCls:'icon-search'" onclick="searchLastShipmentValue()">检索该用户最后出货价格</div>
    </div>
</body>
</html>